<meta charset='utf-8'>
<?php

//file_put_contents('D:\www\DB\1.txt',$_REQUEST);die;
error_reporting(E_ALL ^ E_NOTICE); 
date_default_timezone_set('PRC');

//设置接口参数
$name = 'bjpks';
$uid = '781488';
$token = '76e0587095e4fb073eb4464af8f6028271823950';

//设置缓存文件
$cache_url = "".$name.".txt";

//缓存文件（最后更新时间）
$filemtime = filemtime($cache_url);

//缓存文件（更新频率设置）
$second = '10';

if ( time() - $filemtime > $second ) {
	
    //设置参数 http://api.caipiaokong.cn/lottery/?name=bjpks&format=json&uid=781488&token=76e0587095e4fb073eb4464af8f6028271823950
    $data = file_get_contents("http://api.caipiaokong.com/lottery/?name=".$name."&format=json&uid=".$uid."&token=".$token."");
	
    //$data缓存
    $array = json_decode($data,true);
    if(is_array($array)) {
        file_put_contents($cache_url,$data,LOCK_EX);
    }

}
//读取缓存
$data = file_get_contents($cache_url);
$array = json_decode($data,true);
foreach ($array as $key => $value) {
	$datas[]['expect'] = $key;
	$datas[] = $value;
	//$datas['enddt'] = $value['dateline'];
}

$url = 'http://qt.hiberbest.com/jieshui.php'; 
$post_data['bclass_wd']       = 'pk10';
$post_data['enddt']      = $datas[1]['dateline'];
$post_data['expect'] = $datas[0]['expect'];
$post_data['opencode']    = $datas[1]['number'];
$res = request_post($url, $post_data);       
       
function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        
        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
		
        $postUrl = $url;
        $curlPost = $post_data;

        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }

?>